package com.jinlejia.erp.ui.house.prospect;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.bean.BuildingBean;
import com.jinlejia.erp.bean.RoomBean;
import com.jinlejia.erp.bean.UnitBean;
import com.weight.CustomToast;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 搜索所需要的HTTP 帮助类
 * 2019年7月16日
 */

class SearchHelper {

    private final Context mContext;
    public SearchHelper(Context mContext) {
        this.mContext = mContext;
    }

    //region 根据小区编码搜索座栋信息
    void queryBuildingList(String reCode, final QueryBuildingCallBack callBack) {
        OkHttpUtils.get()
                .url(ApiConfig.HOUSE_SOURCE_QUERY_BUILDING_LIST.concat(reCode))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        CustomToast.showToast(mContext, "网络连接失败", 1500);
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (showEmptyMessage(response)) return;
                        if (DataFilter.filter(mContext, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            JSONArray list = data.getJSONArray("buildingList");
                            List<BuildingBean> beans = JSON.parseArray(list.toString(), BuildingBean.class);
                            callBack.onQueryBuilding(beans);
                        }
                    }
                });
    }

    public interface QueryBuildingCallBack {
        void onQueryBuilding(List<BuildingBean> beans);
    }
    //endregion


    //region 根据座栋编号获取单元集合
    void queryEstateListByBuildingCode(String buildingCode, final UnitCallBack callBack) {
        OkHttpUtils.get()
                .url(ApiConfig.HOUSE_SOURCE_QUERY_UNIT_BY_BUILDING.concat(buildingCode))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        CustomToast.showToast(mContext, "网络连接失败", 1500);
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (showEmptyMessage(response)) return;
                        if (DataFilter.filter(mContext, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            JSONArray list = data.getJSONArray("unitList");
                            List<UnitBean> beans = JSON.parseArray(list.toString(), UnitBean.class);
                            callBack.onUnitResult(beans);
                        }
                    }
                });
    }

    public interface UnitCallBack {
        void onUnitResult(List<UnitBean> beans);
    }
    //endregion


    //region 根据单元编号获取房间号集合
    void queryEstateListByUnitCode(String unitcod, final RoomCallBack callBack) {
        OkHttpUtils.get()
                .url(ApiConfig.HOUSE_SOURCE_QUERYE_HOME_LIST.concat(unitcod))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        CustomToast.showToast(mContext, "网络连接失败", 1500);
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (showEmptyMessage(response)) return;
                        if (DataFilter.filter(mContext, response)) {
                            JSONObject data = JSON.parseObject(response.get("data").toString());
                            JSONArray list = data.getJSONArray("roomList");
                            List<RoomBean> beans = JSON.parseArray(list.toString(), RoomBean.class);
                            callBack.onRoomResult(beans);
                        }
                    }
                });
    }

    public interface RoomCallBack {
        void onRoomResult(List<RoomBean> beans);
    }


    private boolean showEmptyMessage(Map<String, Object> response) {
        if ("501".equals(String.valueOf(response.get("code")))) {
            if (response.get("message") != null)
                CustomToast.showToast(mContext, response.get("message").toString(), 1500);
            return true;
        }
        return false;
    }
}
